<?php

namespace App\Models;

class Orders extends Base
{
    protected $table = 'orders';

    const PAY_STATUS_1 = 1;
    const PAY_STATUS_2 = 2;
    const PAY_STATUS_TEXT = [
        self::PAY_STATUS_1 => '<span class="el-tag el-tag--danger">未支付</span>',
        self::PAY_STATUS_2 => '<span class="el-tag">已支付</span>',
    ];

    const SHIPPING_STATUS_1 = 1;
    const SHIPPING_STATUS_2 = 2;
    const SHIPPING_STATUS_TEXT = [
        self::SHIPPING_STATUS_1 => '<span class="el-tag el-tag--danger">未发货</span>',
        self::SHIPPING_STATUS_2 => '<span class="el-tag">已发货</span>',
    ];

    const ORDER_STATUS_1 = 1;
    const ORDER_STATUS_2 = 2;
    const ORDER_STATUS_TEXT = [
        self::ORDER_STATUS_1 => '<span class="el-tag el-tag--danger">未收货</span>',
        self::ORDER_STATUS_2 => '<span class="el-tag">已收货</span>',
    ];

    const ORDER_FROM_1 = 1;
    const ORDER_FROM_2 = 2;
    const ORDER_FROM_TEXT = [
        self::ORDER_FROM_1 => '<span class="el-tag el-tag--danger">PC</span>',
        self::ORDER_FROM_2 => '<span class="el-tag">手机</span>',
    ];

    const REFUND_STATE_1 = 1;
    const REFUND_STATE_2 = 2;
    const REFUND_STATE_TEXT = [
        self::REFUND_STATE_1 => '<span class="el-tag">无退款</span>',
        self::REFUND_STATE_2 => '<span class="el-tag el-tag--danger">退款</span>',
    ];

    const EVALUATION_STATE_1 = 1;
    const EVALUATION_STATE_2 = 2;
    const EVALUATION_STATE_TEXT = [
        self::EVALUATION_STATE_1 => '<span class="el-tag el-tag--danger">未评价</span>',
        self::EVALUATION_STATE_2 => '<span class="el-tag">已评价</span>',
    ];

    const ORDER_STATE_1 = 1;
    const ORDER_STATE_2 = 2;
    const ORDER_STATE_3 = 3;
    const ORDER_STATE_4 = 4;
    const ORDER_STATE_5 = 5;
    const ORDER_STATE_TEXT = [
        self::ORDER_STATE_1 => '<span class="el-tag">已取消</span>',
        self::ORDER_STATE_2 => '<span class="el-tag el-tag--danger">未付款</span>',
        self::ORDER_STATE_3 => '<span class="el-tag">已付款</span>',
        self::ORDER_STATE_4 => '<span class="el-tag">已发货</span>',
        self::ORDER_STATE_5 => '<span class="el-tag">已收货</span>',
    ];

    protected $fillable = [
        'id','order_sn','pay_sn','user_id','user_name','store_id','store_name','order_number','order_amount','pay_price','pay_status','pay_method',
        'pay_time','shipping_price','shipping_status', 'shipping_time','address_id','reciver_info','shipping_code','shipping_sn','order_status',
        'order_time','order_state','evaluation_state','refund_state','refund_amount','finnshed_time','order_from','status','created_at','updated_at'
    ];

    protected $casts = ['reciver_info' => 'array'];

    /**
     * 获取列表
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function getList($params = [], $fields = ['*'], $limit = 10){
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['kwd']) && $params['kwd']) {
                    $query ->where('order_sn','like','%' . $params['kwd'] . '%');
                }
                if(isset($params['order_state']) && $params['order_state']) {
                    $query ->where('order_state',$params['order_state']);
                }
                if(isset($params['evaluation_state']) && $params['evaluation_state']) {
                    $query ->where('evaluation_state',$params['evaluation_state']);
                }
                if(isset($params['start']) && $params['start']) {
                    $query ->whereDate('created_at','>=',$params['start']);
                }
                if(isset($params['end']) && $params['end']) {
                    $query ->whereDate('created_at','<=',$params['end']);
                }
            })->where('status','<>',-1)
            ->when($params, function ($query) use($params){
                if(isset($params['sort']) && $params['sort']) {
                    return $query->orderBy('order_price',$params['sort']);
                }
            })->orderBy('created_at', 'desc')
            ->paginate($limit);

        $this->stateToText($lists,[
            'pay_status'=> self::PAY_STATUS_TEXT,
            'shipping_status' => self::SHIPPING_STATUS_TEXT,
            'order_status' => self::ORDER_STATUS_TEXT,
            'order_from' => self::ORDER_FROM_TEXT,
            'refund_state' => self::REFUND_STATE_TEXT,
            'evaluation_state' => self::EVALUATION_STATE_TEXT,
            'order_state' => self::ORDER_STATE_TEXT,
        ]);
        return $lists;
    }

    /**
     * 所有数据
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @param int $limit 分页数量
     * @return mixed
     */
    protected function allLists($params = [], $fields = ['*'], $page = null, $limit = 10, $sort = 'id', $orderby = 'desc', $extend = []){
        if(isset($page)){
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['order_sn']) && $params['order_sn']) {
                        $query ->where('order_sn',$params['order_sn']);
                    }
                    if(isset($params['pay_sn']) && $params['pay_sn']) {
                        $query ->where('pay_sn',$params['pay_sn']);
                    }
                    if(isset($params['order_state']) && $params['order_state']) {
                        $query ->where('order_state',$params['order_state']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->paginate($limit, null, null, $page);
        }else{
            $lists = $this->select($fields)
                ->where(function ($query) use ($params) {
                    if(isset($params['user_id']) && $params['user_id']) {
                        $query ->where('user_id',$params['user_id']);
                    }
                    if(isset($params['order_sn']) && $params['order_sn']) {
                        $query ->where('order_sn',$params['order_sn']);
                    }
                    if(isset($params['pay_sn']) && $params['pay_sn']) {
                        $query ->where('pay_sn',$params['pay_sn']);
                    }
                    if(isset($params['order_state']) && $params['order_state']) {
                        $query ->where('order_state',$params['order_state']);
                    }
                })->where('status','<>',-1)
                ->orderBy($sort, $orderby)->get();
        }
//        if (!empty($lists)){
//            foreach ($lists as $order) {
//                if (isset($order['order_state'])) {
//                    if($order['order_state'] == 1){
//                        $order['state_desc'] = '已取消';
//                    }elseif ($order['order_state'] == 2){
//                        $order['state_desc'] = '未付款';
//                    }elseif ($order['order_state'] == 3){
//                        $order['state_desc'] = '已付款';
//                    }elseif ($order['order_state'] == 4){
//                        $order['state_desc'] = '已发货';
//                    }elseif ($order['order_state'] == 5){
//                        $order['state_desc'] = '已收货';
//                    }
//                }
//            }
//        }
        return $lists;
    }


    /**
     * 取单条订单信息
     * @access public
     * @author csdeshang
     * @param array $params 条件
     * @param array $extend 扩展
     * @param string $fields 字段
     * @return array
     */
    protected function getOrderInfo($params = [], $fields = ['*']) {
        $lists = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['id']) && $params['id']) {
                    $query ->where('id',$params['id']);
                }
                if(isset($params['user_id']) && $params['user_id']) {
                    $query ->where('user_id',$params['user_id']);
                }
                if(isset($params['pay_sn']) && $params['pay_sn']) {
                    $query ->where('pay_sn',$params['pay_sn']);
                }
                if(isset($params['order_state']) && $params['order_state']) {
                    $query ->where('order_state',$params['order_state']);
                }
            })->where('status','<>',-1)->first();

        return $lists;
    }

    /**
     * 根据id查询一条信息
     * @param array $params 条件参数
     * @param array $fields 查询的字段
     * @return mixed
     */
    protected function details($params = [], $fields = ['*']){
        return $this->select($fields)->where($params)->where('status','<>',-1)->first();
    }

    /**
     * 获取条数
     * @param Request $request
     * @return bool
     */
    protected function getCounts($params = [], $fields = ['*']){
        $counts = $this->select($fields)
            ->where(function ($query) use ($params) {
                if(isset($params['user_id']) && $params['user_id']) {
                    $query ->where('user_id',$params['user_id']);
                }
                if(isset($params['order_state']) && $params['order_state']) {
                    $query ->where('order_state',$params['order_state']);
                }
            })->where('status','<>',-1)->count();
        return $counts;
    }


    /**
     * 取消订单
     * @param array $order_info
     * @return array
     */
    protected function changeOrderStateCancel($order_info)
    {
        $order_id = $order_info['id'];
        //库存销量变更
        $goods_list =OrderGoods::where('order_id',$order_id)->get(['goods_id','goods_num']);
        $data = [];
        foreach ($goods_list as $goods) {
            $data[$goods['goods_id']] = $goods['goods_num'];
        }
        foreach ($data as $goods_id => $number) {
            Goods::where('id',$goods_id)->increment('storage',$number);
        }
        if($order_info['order_state'] == 3){
            //当是已付款,未发货状态,则直接取消订单, 金额退回余额.
            if ($order_info['order_amount'] > 0) {
                Users::where('id',$order_info['user_id'])->increment('balance',$order_info['order_amount']);
            }
            //发送站内信-余额变更通知
            $template = [
                'title' => '余额增加通知',
                'content' => '您的余额增加："' . $order_info['order_amount'] .'"，该金额为取消订单退款。订单号为：'. $order_info['order_sn'],
                'user_id' => $order_info['user_id'],
                'status' => 1,
                'type' => 1,
            ];
            Mails::create($template);
            //添加余额日志记录
            $integral_arr = [
                'user_id' => $order_info['user_id'],
                'user_name' => $order_info['user_name'],
                'balance' => $order_info['order_amount'],
                'desc' => '取消订单退还余额',
            ];
            BalanceLogs::create($integral_arr);
        }
        //更新订单信息
        $update_order = ['order_state' => 1,'finnshed_time'=>date('Y-m-d H:i:s')];
        $this->where('id',$order_id)->update($update_order);
    }



}
